﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ParseData;
using NHibernateDB;
//using Grafic;
using System.Collections;

namespace GuiSenzori
{
    public partial class SenzorGUI : Form
    {
        private static SenzorGUI instance;

        public static string nume = string.Empty;
        public static string prenume = string.Empty;
        public static string CNP = string.Empty;
        public static DateTime dataInceput;
        public static DateTime dataSfarsit;
        public static int idsenzor = 0;

        // Threads
        private TestReader tr;
        private Thread t;
        // List of attached sensors.
        public List<ISensor> sensors = new List<ISensor>();
        int _previouslySortedColumn = -1;
        string limba;
        Dictionary<string, int> senzordict = new Dictionary<string, int>();
        string ip;
        string port;


        public SenzorGUI(string limba,string ip, string port)
        {
            instance = this;
            InitializeComponent();
            Rename(limba);
          
            this.limba = limba;
            this.ip = ip;
            this.port = port;

            tr = new TestReader(ip, port, limba);

            cbSenzori.Enabled = false;
            buttonStartSenzor.Enabled = false;
            buttonStopSenzor.Enabled = false;
            buttonGrafice.Enabled = false;
            //buttonSelectarePacient.Enabled = false;

            // Adding columns to ListView Pacienti
            listaPacientiSenzor.View = View.Details;
            listaPacientiSenzor.FullRowSelect = true;
            listaPacientiSenzor.GridLines = false;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(listaPacientiSenzor.Name.ToString(), string.Empty, string.Empty, "name");

            foreach (string s in namesForm)
            {
                listaPacientiSenzor.Columns.Add(s);
              
            }

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> lista = pm.RetrieveAll<DateGeneralePacient>(SessionAction.BeginAndEnd);
            listaPacientiSenzor.Items.Clear();
            populateListView(lista);

            pm = PersistenceManager.Instance;
            IList<Senzori> listSenzori = pm.RetrieveAll<Senzori>(SessionAction.BeginAndEnd);

            foreach (Senzori s in listSenzori)
            {
                cbSenzori.Items.Add(s.NumeSenzor);
                senzordict.Add(s.NumeSenzor, s.IdSenzor);
            }
        }


        void listview1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {

            ListViewComparer lvComparer;
            if (null == listaPacientiSenzor.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)listaPacientiSenzor.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumn)
            {
                listaPacientiSenzor.Sorting = SortOrder.None;
            }
            if (listaPacientiSenzor.Sorting == SortOrder.Ascending)
            {
                listaPacientiSenzor.Sorting = SortOrder.Descending;
            }
            else
            {
                listaPacientiSenzor.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(listaPacientiSenzor, ref _previouslySortedColumn, e.Column);
            lvComparer.SortColumn = e.Column;
            listaPacientiSenzor.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.listaPacientiSenzor.Columns)
            {
                ch.Width = -2;
            }
        }


        // Populate ListView with BD datas
        private void populateListView(IList<DateGeneralePacient> list)
        {
            foreach (DateGeneralePacient dgp in list)
            {
                ListViewItem item = new ListViewItem(dgp.IdPacient);
                item.SubItems.Add(dgp.Nume);
                item.SubItems.Add(dgp.Prenume);
                item.SubItems.Add(dgp.Sex);
                item.SubItems.Add(dgp.DataNasterii.ToString());
                listaPacientiSenzor.Items.Add(item);
            }
            //adi
            foreach (ColumnHeader ch in this.listaPacientiSenzor.Columns)
            {
                ch.Width = -2;
            }
            //adi end


        }



        // Button "Start senzor"
        private void buttonStartSenzor_Click(object sender, EventArgs e)
        {
            listaPacientiSenzor.Enabled = false;
            cbSenzori.Enabled = false;

            if (cbSenzori.Text.Equals("Alegere senzori"))
            {
                XMLParser xml = new XMLParser(@"C:\Language" + limba + ".xml");
                Dictionary<string, string> dict = xml.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

                MessageBox.Show(dict["alegeSenzor"], dict["TitluEroare"], MessageBoxButtons.OK, MessageBoxIcon.Error);

                listaPacientiSenzor.Enabled = true;
                cbSenzori.Enabled = true;
            }
            else
            {
                TestReader.CNP = CNP;
                t = new Thread(new ThreadStart(tr.readData));
                t.Name = "currentThread";
                tr.dataSetata = false;
                //t.Start();

                if (senzordict[cbSenzori.Text] == 100001 || senzordict[cbSenzori.Text] == 100002)
                {
                    TestReader.addSensor(SensorFactory.createFromFile(@"..\..\..\Datas\senzor_temp.txt"));
                    TestReader.addGraficTemperatura(CNP, nume, prenume, limba);
                    buttonStopSenzor.Enabled = true;
                    buttonStartSenzor.Enabled = false;
                    buttonGrafice.Enabled = true;
                }
                else if (senzordict[cbSenzori.Text] == 200001)
                {
                    TestReader.addSensor(SensorFactory.createFromFile(@"..\..\..\Datas\senzor_light.txt"));
                    TestReader.addGraficLumina(CNP, nume, prenume, limba);
                    buttonStopSenzor.Enabled = true;
                    buttonStartSenzor.Enabled = false;
                    buttonGrafice.Enabled = true;
                }
                else if (senzordict[cbSenzori.Text] == 300001)
                {
                    TestReader.addSensor(SensorFactory.createFromFile(@"..\..\..\Datas\senzor_ECG.txt"));
                    TestReader.addGraficECG(CNP, nume, prenume, limba);
                    buttonStopSenzor.Enabled = true;
                    buttonStartSenzor.Enabled = false;
                    buttonGrafice.Enabled = true;
                }
                else if (senzordict[cbSenzori.Text] == 400001)
                {
                    TestReader.addSensor(SensorFactory.createFromFile(@"..\..\..\Datas\senzor_BP.txt"));
                    TestReader.addGraficBP(CNP, nume, prenume, limba);
                    buttonStopSenzor.Enabled = true;
                    buttonStartSenzor.Enabled = false;
                    buttonGrafice.Enabled = true;
                }

                t.Start();
            }

        }


        // Button "Stop senzor"
        private void buttonStopSenzor_Click(object sender, EventArgs e)
        {
            t.Abort();
            TestReader.removeAllSensors();
            buttonStopSenzor.Enabled = false;
            buttonStartSenzor.Enabled = true;
            listaPacientiSenzor.Enabled = true;
            cbSenzori.Enabled = true;

            InvestigatiiSenzor ivsen = new InvestigatiiSenzor();
            ivsen.DataInceput = tr.dataInceput;
            ivsen.DataSfarsit = tr.dataSfarsit;
            ivsen.IdPacient = CNP;
            ivsen.IdSenzor = senzordict[cbSenzori.Text];

            PersistenceManager pm = PersistenceManager.Instance;
            pm.Save(ivsen);
        }

        // Click event for ListView Pacienti
        private void listaPacienti_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < listaPacientiSenzor.Items.Count; i++)
            {
                if (listaPacientiSenzor.Items[i].Selected == true)
                {
                    ListViewItem itm = listaPacientiSenzor.SelectedItems[0];
                    SenzorGUI.CNP = itm.SubItems[0].Text;
                    SenzorGUI.nume = itm.SubItems[1].Text;
                    SenzorGUI.prenume = itm.SubItems[2].Text;
                    SenzorGUI.ActualizarePacientNume();
                    break;
                }
            }
            cbSenzori.Enabled = true;
            buttonStartSenzor.Enabled = true;
        }

        // Dispaling in a label pacient info
        public static void ActualizarePacientNume()
        {
            if (!CNP.Equals(string.Empty))
            {
                instance.lbPacientSelectat.Text = "Pacientul selectat: " + nume + " " + prenume;
                instance.lbPacientSelectat.Visible = true;
            }
            else
            {
                instance.lbPacientSelectat.Text = "";
            }
        }

        // Button "Grafice
        private void buttonGrafice_Click(object sender, EventArgs e)
        {
            if (senzordict[cbSenzori.Text] == 100001 || senzordict[cbSenzori.Text] == 100002)
            {
                TestReader.displayGraficTemperatura();
            }
            else if (senzordict[cbSenzori.Text] == 200001)
            {
                TestReader.displayGraficLumina();
            }
            else if (senzordict[cbSenzori.Text] == 300001)
            {
                TestReader.displayGraficECG();
            }
            else if (senzordict[cbSenzori.Text] == 400001)
            {
                TestReader.displayGraficBP();
            }
        }
        // Button "close"
        protected override void OnClosed(EventArgs e)
        {
            if (t != null)
                t.Abort();
            base.OnClosed(e);
        }
    }
}
